package digui;

 class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
 }
public class Text2 {

    //题目 ： 力扣 129题  求根节点到叶子节点数字之和
    //算法  ： dfs  深搜  递归

    public int sumNumbers(TreeNode root) {
        //设置函数头
        return dfs(root,0);
    }

    public static int dfs(TreeNode root,int prevSum) {
        if(root == null) {
            return 0;
        }

        //算一下当前到这里的和
        int sum = prevSum * 10 + root.val;

        if(root.left == null && root.right == null) {
            return sum;
        }
        return dfs(root.left,sum) + dfs(root.right,sum);
    }
    public static void main(String[] args) {

    }
}
